package com.tbvtc.agriculturalcreditchainserver2025.controller.admin;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tbvtc.agriculturalcreditchainserver2025.entity.User;
import com.tbvtc.agriculturalcreditchainserver2025.exception.FoodException;
import com.tbvtc.agriculturalcreditchainserver2025.service.IFarmlandInfoService;
import com.tbvtc.agriculturalcreditchainserver2025.service.IUserService;
import com.tbvtc.agriculturalcreditchainserver2025.service.LargeDateScreenService;
import com.tbvtc.agriculturalcreditchainserver2025.utils.*;
import com.tbvtc.agriculturalcreditchainserver2025.vo.LargeDateScreen.RevenueAnalysisVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.LargeDateScreen.UserInfo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.Pest.getCropPestReportVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Slf4j
@Tag(name = "数据大屏")
@RequestMapping("/largeDateScreen")
public class LargeDateScreenController {
    @Autowired
    private LargeDateScreenService largeDateScreenService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IFarmlandInfoService farmlandInfoService;

    @Operation(summary = "查询土地总面积以及耕种面积")
    @GetMapping("/getLandAreaAndCount")
    @Parameters({
            @Parameter(name = "id", description = "用户id")
    })
    public Result getLandAreaAndCount(Integer id) {
        log.info("查询土地总面积以及耕种数量");
        Map map = farmlandInfoService.selectLandArea(id);
        return RUtils.success(map);
    }

    @Operation(summary = "查询用户个人信息")
    @GetMapping("/getUserInfo")
    @Parameters({
            @Parameter(name = "id", description = "用户id")
    })
    public Result getUserInfo(Integer id) {
        log.info("查询用户个人信息");
        UserInfo userInfo = farmlandInfoService.selectUserInfo(id);
        return RUtils.success(userInfo);
    }

    @Operation(summary = "存证数量和最近存证")
    @GetMapping("/getEvidenceCountAndgetRecentEvidence")
    public Result getEvidenceCount() {
        Map map = largeDateScreenService.getEvidenceCountAndgetRecentEvidence();
        return RUtils.success(map);
    }

    @Operation(summary = "作物名称，土地，生长状态")
    @PostMapping("/cropInfo")
    public Result cropInfo() {
        log.info("作物名称，土地，生长状态");
        List info = largeDateScreenService.getCropInfo();
        return RUtils.success(info);
    }

    @Operation(summary = "更换头像")
    @PostMapping("/changeUserImage")
    public Result changeUserImage(Integer id, String url) {
        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<User>()
                .eq(User::getId, id)
                .set(User::getUserImage, url);
        boolean b = userService.update(lambdaUpdateWrapper);
        if (!b) {
            throw new FoodException(ResultCodeEnum.FAILE_MODIFY_PROFILE_POCTURE);
        }
        return RUtils.success();
    }

    @Operation(summary = "收入分析")
    @PostMapping("/RevenueAnalysis")
    @Parameter( name = "phone", description = "手机号")
    public Result RevenueAnalysis(String phone){
        RevenueAnalysisVo  revenueAnalysis = largeDateScreenService.getRevenueAnalysis(phone);
        return RUtils.success(revenueAnalysis);
    }
    @Operation(summary = "病虫害查询")
    @GetMapping("/pestInfo")
    @Parameter(name = "userId", description = "农户ID")
    public Result pestInfo(Integer userId)   {
        List<getCropPestReportVo>  reportVo = largeDateScreenService.pestInfo(userId);
        return RUtils.success(reportVo);
    }
    @Operation(summary = "取消病虫害")
    @PostMapping("/cancelPest")
    @Parameter(name = "name", description = "农作物name")
    public Result cancelPest(String name)   {
        largeDateScreenService.cancelPest(name);
        return RUtils.success();
    }

}
